//
//  UIView+HDLockHomeView.m
//  app
//
//  Created by pro on 2017/4/11.
//  Copyright © 2017年 hdaren. All rights reserved.
//

#import "HDLockHomeView.h"
#import "QRCodeGenerator.h"
#import "GattManager.h"

@interface HDLockHomeView ()

@property(strong,nonatomic) GattManager *gattManager;

@end

@implementation  HDLockHomeView

-(instancetype)initWithFrame:(CGRect)frame
{
    if (self = [super initWithFrame:frame]) {
        
        self.gattManager = [[GattManager alloc] init];
        [self creatSubViews];
        
    }
    return self;
}

-(void)creatSubViews
{
    UIImageView * back_iv = [[UIImageView alloc]initWithFrame:CGRectOf([self.ubo fx:0 fy:0], [self.ubo ow:UBO_SCREEN.width oh:UBO_SCREEN.height])];
    //back_iv.image = [UIImage imageNamed:@""];
    back_iv.backgroundColor = [XCommon hexStringToColor:kColor_back];
    back_iv.userInteractionEnabled = YES;
    [self addSubview:back_iv];
    
    
    UIView * er_code_view = [[UIView alloc]initWithFrame:CGRectOf([back_iv.ubo fx:0 ox:12 fy:0 oy:6], [back_iv.ubo ow:UBO_SCREEN.width-24 oh:UBO_SCREEN.width-24])];
    er_code_view.userInteractionEnabled = YES;
    er_code_view.backgroundColor = [UIColor whiteColor];
    [back_iv addSubview:er_code_view];
    
    
    UILabel * tip_lab = [[UILabel alloc]initWithFrame:CGRectOf([er_code_view.ubo fx:UB_1_2 ox:-(UBO_SCREEN.width-24)/2 fy:0 oy:0], [er_code_view.ubo ow:UBO_SCREEN.width-24 oh:28])];
    tip_lab.text = @"为了安全起见，本码每天只能扫3次";
    [tip_lab setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
    [tip_lab setFont:[UIFont systemFontOfSize:12]];
    [tip_lab setTextAlignment:NSTextAlignmentCenter];
    [er_code_view addSubview:tip_lab];
    tip_lab.hidden = YES;
    
    
    CGFloat er_code_iv_width = er_code_view.frame.size.width-36;
    self.er_code_iv = [[UIImageView alloc]initWithFrame:CGRectOf([tip_lab.ufo fx:UB_1_2 ox:-er_code_iv_width/2 fy:1 oy:-24], [er_code_view.ubo ow:er_code_iv_width oh:er_code_iv_width])];
   // self.er_code_iv.backgroundColor = [UIColor blackColor];
    self.er_code_iv.image = [UIImage imageNamed:@"defaulterweima"];
    [er_code_view addSubview:self.er_code_iv];
    
    
//    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"erweima_key"]length] > 0) {
//        
//        NSArray * key = @[[[NSUserDefaults standardUserDefaults]objectForKey:@"erweima_key"]];
//        NSString *qrCode = [self.gattManager getQRCode:key refreshInterval:180];
//        self.er_code_iv.image = [QRCodeGenerator qrImageForString:qrCode imageSize:self.er_code_iv.bounds.size.width];
//    }
    
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"erweima_key"]count] > 0) {
        
        NSArray * key = [[NSUserDefaults standardUserDefaults]objectForKey:@"erweima_key"];
     //   NSString *qrCode = [self.gattManager createElevatorControllQRWithLLingId:@"0001000523" Keys:key ValidTime:3 AuthLevel:1 EncryptKey:@"12345678"];
        
        NSString *qrCode = [self.gattManager createElevatorControllQRWithLLingId:[[NSUserDefaults standardUserDefaults]objectForKey:@"linglingID"] Keys:key ValidTime:10 ValidNum:3 AuthLevel:0 EncryptKey:@"12345678"];

        
        self.er_code_iv.image = [QRCodeGenerator qrImageForString:qrCode imageSize:self.er_code_iv.bounds.size.width];
    }
    
    
    UIImageView * icon_iv = [[UIImageView alloc]initWithFrame:CGRectOf([self.er_code_iv.ufo fx:UB_1_2 ox:-50/2 fy:UB_1_2 oy:-50/2],[er_code_view.ubo ow:50 oh:50])];
    icon_iv.image = [UIImage imageNamed:@""];
    icon_iv.backgroundColor = [UIColor redColor];
    //[er_code_view addSubview:icon_iv];
    
    
    self.lock_home_btn = [[UIButton alloc]initWithFrame:CGRectOf([er_code_view.ufo fx:UB_1_2 ox:-80/2 fy:1 oy:-80/2], [back_iv.ubo ow:80 oh:80])];
    //lock_home_btn.backgroundColor = [UIColor orangeColor];
   // [lock_home_btn setTitle:@"开" forState:UIControlStateNormal];
    
    //lock_home_btn.layer.masksToBounds = YES;
   // lock_home_btn.layer.cornerRadius = lock_home_btn.bounds.size.width/2;
    [self.lock_home_btn addTarget:self action:@selector(lockHome:) forControlEvents:UIControlEventTouchUpInside];
    [back_iv addSubview:self.lock_home_btn];
    
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"linglingID"]length] > 0  ) {
        [self.lock_home_btn setBackgroundImage:[UIImage imageNamed:@"video_recorder_start_btn_open"] forState:UIControlStateNormal];
    }else
    {
        [self.lock_home_btn setBackgroundImage:[UIImage imageNamed:@"video_recorder_start_btn_activate"] forState:UIControlStateNormal];
    }
    
    
    self.bottom_lab = [[UILabel alloc]initWithFrame:CGRectOf([self.lock_home_btn.ufo fx:UB_1_2 ox:-(UBO_SCREEN.width-60)/2 fy:1 oy:50], [back_iv.ubo ow:UBO_SCREEN.width-60 oh:50])];
    
    [self.bottom_lab setFont:[UIFont systemFontOfSize:12]];
    [self.bottom_lab setTextColor:[XCommon hexStringToColor:kColor_l_light_gray_text]];
    [self.bottom_lab setTextAlignment:NSTextAlignmentCenter];
    self.bottom_lab.numberOfLines = 0;
    self.bottom_lab.lineBreakMode = NSLineBreakByClipping;
    [back_iv addSubview:self.bottom_lab];
    
    if ([[[NSUserDefaults standardUserDefaults]objectForKey:@"linglingID"]length] > 0  ) {
    
        self.bottom_lab.text = @"告诉你一个秘密手机摇一摇也可以开门哦！本二维码可在10分钟使用3次，您再次进入系统会重新生成!";
    
    }else
    {
        self.bottom_lab.text = @"如您小区安装有手机开门设备，请联系物业索取二维码";
    }
    
    
    
    UILabel * jihuo_lab = [[UILabel alloc]initWithFrame:CGRectOf([er_code_view.ufo fx:0 fy:1 oy:12], [back_iv.ubo ow:150 oh:40])];
    jihuo_lab.text = @"切换账户";
    [jihuo_lab setTextColor:[UIColor orangeColor]];
    [jihuo_lab setFont:[UIFont systemFontOfSize:13]];
    [jihuo_lab setTextAlignment:NSTextAlignmentLeft];
    [back_iv addSubview:jihuo_lab];
    
    
    UIButton * jihuo_btn = [[UIButton alloc]initWithFrame:CGRectOf([er_code_view.ufo fx:0 fy:1 oy:12], [back_iv.ubo ow:120 oh:40])];
//    [jihuo_btn setTitle:@"去激活/重新验证" forState:UIControlStateNormal];
//    [jihuo_btn setTitleColor:[UIColor orangeColor] forState:UIControlStateNormal];
//    [jihuo_btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
//    [jihuo_btn.titleLabel setTextAlignment:NSTextAlignmentLeft];
    [jihuo_btn addTarget:self action:@selector(jihuo:) forControlEvents:UIControlEventTouchUpInside];
//    jihuo_btn.backgroundColor = [UIColor greenColor];
    [back_iv addSubview:jihuo_btn];
    
    
    
    UILabel * fangke_lab = [[UILabel alloc]initWithFrame:CGRectOf([er_code_view.ufo fx:1 ox:-100 fy:1 oy:12], [back_iv.ubo ow:100 oh:40])];
    fangke_lab.text = @"访客授权";
    [fangke_lab setFont:[UIFont systemFontOfSize:13]];
    [fangke_lab setTextColor:[XCommon hexStringToColor:kColor_red_text]];
    [fangke_lab setTextAlignment:NSTextAlignmentRight];
//    [back_iv addSubview:fangke_lab];
    
    
    
    UIButton * fangke_btn = [[UIButton alloc]initWithFrame:CGRectOf([er_code_view.ufo fx:1 ox:-100 fy:1 oy:12], [back_iv.ubo ow:80 oh:40])];
//    [fangke_btn setTitle:@"访客授权" forState:UIControlStateNormal];
//    [fangke_btn setTitleColor:[XCommon hexStringToColor:kColor_red_text] forState:UIControlStateNormal];
//    [fangke_btn.titleLabel setFont:[UIFont systemFontOfSize:12]];
 //   [fangke_btn.titleLabel setTextAlignment:NSTextAlignmentRight];
    [fangke_btn addTarget:self action:@selector(fangke:) forControlEvents:UIControlEventTouchUpInside];
//    fangke_btn.backgroundColor = [UIColor blueColor];
    [back_iv addSubview:fangke_btn];

    
    
    
    
    
    
    
    
    
    
}


-(void)lockHome:(UIButton*)sender
{
    [MyToast showWithText:@"敬请期待"];
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(lockHomeFounction)]) {
//        [self.delegate lockHomeFounction];
//    }
}


-(void)jihuo:(UIButton*)sender
{
    [MyToast showWithText:@"敬请期待"];
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(jihuoFounction)]) {
//        [self.delegate jihuoFounction];
//    }
}

-(void)fangke:(UIButton*)sender
{
    [MyToast showWithText:@"敬请期待"];
    
//    if (self.delegate && [self.delegate respondsToSelector:@selector(fangkeFounction)]) {
//        [self.delegate fangkeFounction];
//    }
}



-(UIImage *)getBarPic:(NSString*)str
{
    //根据虚拟订单号生成二维码
//    dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
        // 1.创建过滤器
        CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
        // 2.恢复默认
        [filter setDefaults];
        // 3.给过滤器添加数据
        NSString *dataString = [[NSUserDefaults standardUserDefaults]objectForKey:str];
        NSData *data = [dataString dataUsingEncoding:NSUTF8StringEncoding];
        // 4.通过KVO设置滤镜inputMessage数据
        [filter setValue:data forKeyPath:@"inputMessage"];
        // 4.获取输出的二维码
        CIImage *outputImage = [filter outputImage];
        // 5.将CIImage转换成UIImage，并放大显示
        
        UIImage * image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
        
        
        return image;
//        dispatch_async(dispatch_get_main_queue(), ^{
//            barcode.barcodeIV.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:200];
//            
//            UIImageView * logoImageView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:K_Other_ehuigou_logo]];
//            
//            if ([[HDDeviceInfo share]getDeviceIsPhone]) {
//                logoImageView.frame = CGRectOf([barcode.barcodeIV.ubo fx:UB_1_2 ox:-50/2 fy:UB_1_2 oy:-50/2],[barcode.barcodeIV.ubo ow:50 oh:50]);
//            }else{
//                logoImageView.frame = CGRectOf([barcode.barcodeIV.ubo fx:UB_1_2 ux:-(barcode.barcodeIV.frame.size.width/5-10)/2 fy:UB_1_2 uy:-(barcode.barcodeIV.frame.size.width/5-10)/2],[barcode.barcodeIV.ubo uw:barcode.barcodeIV.frame.size.width/5-10 uh:barcode.barcodeIV.frame.size.width/5-10]);
//            }
//            [barcode.barcodeIV addSubview:logoImageView];
//        });
//    });
}

- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    
    //设置比例
    
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    
    // 创建bitmap（位图）;
    
    size_t width = CGRectGetWidth(extent) * scale;
    
    size_t height = CGRectGetHeight(extent) * scale;
    
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    
    CIContext *context = [CIContext contextWithOptions:nil];
    
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    
    CGContextScaleCTM(bitmapRef, scale, scale);
    
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    
    // 保存bitmap到图片
    
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    
    CGContextRelease(bitmapRef);
    
    
    
    CGImageRelease(bitmapImage);
    
    return [UIImage imageWithCGImage:scaledImage];
    
}


@end
